package com.jsy.mini12306.order.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jsy.mini12306.common.entity.TicketOrder;
import com.jsy.mini12306.common.entity.TicketOrderIterm;
import com.jsy.mini12306.order.mapper.StationMapper;
import com.jsy.mini12306.order.mapper.TicketOrderItermMapper;
import com.jsy.mini12306.order.mapper.TicketOrderMapper;
import com.jsy.mini12306.order.vo.TicketOrderItermVO;
import com.jsy.mini12306.order.vo.TicketOrderVO;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author: SongyangJi
 * @description:
 * @since: 2021/9/9
 */

@Component
public class TicketOrderQueryService {

    @Resource
    TicketOrderMapper ticketOrderMapper;

    @Resource
    StationMapper stationMapper;


    @Resource
    TicketOrderItermMapper orderItermMapper;


    private List<TicketOrderVO> getByBuyerId(Long buyerId) {
        List<TicketOrder> ticketOrders = ticketOrderMapper.selectList(new QueryWrapper<TicketOrder>()
                .eq("buyer_id", buyerId));


        List<TicketOrderVO> ticketOrderVOS = new ArrayList<>();
        ticketOrders.forEach(ticketOrder -> {
            TicketOrderVO ticketOrderVO = TicketOrderVO.builder()
                    .orderId(ticketOrder.getOrderId())
                    .buyerId(ticketOrder.getBuyerId())
                    .dateTime(ticketOrder.getDateTime())
                    .trainId(ticketOrder.getTrainId())
                    .departDate(ticketOrder.getDepartDate())
                    .startStation(stationMapper.selectById(ticketOrder.getStartStationId()))
                    .endStation(stationMapper.selectById(ticketOrder.getEndStationId()))
                    .trainName(ticketOrder.getTrainName())
                    .orderNum(ticketOrder.getOrderNum()).build();
            ticketOrderVOS.add(ticketOrderVO);
        });

        ticketOrderVOS.forEach(ticketOrder -> {
            Long orderId = ticketOrder.getOrderId();
            List<TicketOrderIterm> ticketOrderIterms = orderItermMapper.selectList(new QueryWrapper<TicketOrderIterm>()
                    .eq("order_id", orderId));
            List<TicketOrderItermVO> orderItermVOS = new ArrayList<>();
            ticketOrderIterms.forEach(ticketOrderIterm -> {
                orderItermVOS.add(new TicketOrderItermVO(ticketOrderIterm));
            });
            ticketOrder.setTicketOrderIterms(orderItermVOS);
        });

        return ticketOrderVOS;
    }


    // 历史订单
    public List<TicketOrderVO> getHistoriesByBuyerId(Long buyerId) {
        List<TicketOrderVO> ticketOrders = getByBuyerId(buyerId);
        // 日期过滤
        ticketOrders = ticketOrders.stream().filter(ticketOrder -> ticketOrder.getDepartDate().isBefore(LocalDate.now())).collect(Collectors.toList());

        return ticketOrders;
    }

    // 未出行订单
    public List<TicketOrderVO> getUnusedByBuyerId(Long buyerId) {
        List<TicketOrderVO> ticketOrders = getByBuyerId(buyerId);
        // 日期过滤
        ticketOrders = ticketOrders.stream().filter(ticketOrder -> ticketOrder.getDepartDate().isAfter(LocalDate.now())).collect(Collectors.toList());

        return ticketOrders;
    }


}
